var SpiderResultPage = Base.extend({

    popup: null,
    resultTable: null,
    linkNew: null,
    linkBack: null,
    ajaxNewHandlerUrl: null,
    requestParam: null,

    constructor: function()
    {
        this.linkNew = $('#linkNew');
        this.resultTable = new ResultTable();
        this.resultTable.addListener('formArrived', this, this.editFormArrivalHandler);
        this.resultTable.addListener('showTableArrived', this, this.showTableArrivalHandler);
        this.ajaxNewHandlerUrl = $('#newAjaxHandlerUrl').text();
        this.linkNew.bind('click', handler(this, 'createNewTableRecord'));
    },

    refreshPage: function()
    {
        this.popup.hide();
        this.resultTable.refresh();
    },

    showTableArrivalHandler: function(resp)
    {
    	this.addCommonFormInterface(resp);
    	$('#editButton').bind('click', handler(this, 'showTableEditButtonClickHandler'));
    },

    showTableEditButtonClickHandler: function(evt)
    {
    	evt.preventDefault();
    	this.requestParam = $(evt.target).attr('tabindex');
    	this.popup.hide();
    	this.popup.addListener('hidden', this, this.onPopupHide);
    },

    onPopupHide: function()
    {
    	this.popup.removeListener('hidden', this, this.onPopupHide);
    	this.resultTable.requestEditForm(this.requestParam);
    },

    editFormArrivalHandler: function(resp)
    {
    	this.addCommonFormInterface(resp);
        var editForm = new EditForm(true);
        editForm.addListener('update', this, this.refreshPage);
        editForm.addListener('delete', this, this.refreshPage);
    },

    createFormArrivalHandler: function(resp)
    {
    	this.addCommonFormInterface(resp);
        var editForm = new EditForm(false);
        editForm.addListener('create', this, this.refreshPage);
    },

    createNewTableRecord: function(evt)
    {
        evt.preventDefault();
        $.post(
        	this.ajaxNewHandlerUrl,
        	1,
        	handler(this, 'createFormArrivalHandler')
        );
    },

    addCommonFormInterface: function(formContent)
    {
        this.popup = new PopupBox(formContent);
        this.popup.show();
        this.linkBack = $('#backToList');
        this.linkBack.bind('click', handler(this.popup, 'hide'));
    }

});

$(document).ready(init);

function init()
{
    var spiderResultPage = new SpiderResultPage();
}




